import 'package:flutter/material.dart';
import 'list_item.dart';
import 'package:provider/provider.dart';
import 'package:do_it_app/models/task_data.dart';

class TasksList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer<TaskData>(builder: (context, taskData, child) {
      return ListView.builder(
        itemBuilder: (context, index) {
          final task = taskData.tasks[index];
          return ListItem(
            itemName: task.name,
            isChecked: task.isDone,
            toggleCheck: (checkState) {
              taskData.checkTask(task);
            },
            removeTask: () {
              return showDialog(
                  context: context,
                  barrierDismissible: true,
                  builder: (BuildContext context) {
                    return AlertDialog(
                      title: Text('提示'),
                      content: Text("确定删除该待办项?"),
                      actions: <Widget>[
                        FlatButton(
                          child: Text('取消'),
                          onPressed: () {
                            Navigator.of(context).pop();
                          },
                        ),
                        FlatButton(
                          child: Text('确定'),
                          onPressed: () {
                            taskData.deleteTask(task);
                            Navigator.of(context).pop();
                          },
                        ),
                      ],
                    );
                  });
            },
          );
        },
        itemCount: taskData.tasks.length,
      );
    });
  }
}
